package com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions;

import java.util.Date;
import java.util.Map;

import com.gitee.magic.core.utils.StringUtils;
import com.gitee.magic.jdbc.persistence.source.jdbc.mapper.SqlTemplate;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.query.QueryWrapper;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.update.DeleteWrapper;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.update.UpdateWrapper;

/**
 * @author start
 */
public class SqlWrapperBuilder {

	public static final String SELECT="Select %s From %s";
	public static final String UPDATE="Update %s Set %s";
	public static final String DELETE="Delete From %s";
	public static final String WHERE=" Where %s";
	
	public static String getSql(String tableName,QueryWrapper wrapper) {
		return getSql(tableName,wrapper,true);
	}
	
	public static String getSql(String tableName,QueryWrapper wrapper,Boolean flag) {
		String columns=wrapper.getSqlSelect();
		if(StringUtils.isEmpty(columns)) {
			columns="*";
		}
		StringBuilder sbSql=new StringBuilder(String.format(SELECT,columns,tableName));
		if(wrapper.nonEmptyOfWhere()) {
			String sql=wrapper.getSqlNormal();
			sbSql.append(String.format(WHERE,SqlTemplate.getExecuteSql(sql)));
		}
		if(flag) {
			sbSql.append(wrapper.getSqlNormalAfter());
		}
		return sbSql.toString();
	}
	
	public static Object[] getParams(QueryWrapper wrapper) {
		Map<String,Object> params=wrapper.getParamNameValuePairs();
		return SqlTemplate.getExecuteParameter(wrapper.getSqlSegment(), params);
	}
	
	public static String getSql(String tableName,UpdateWrapper wrapper) {
		String sqlSet=wrapper.getSqlSet();
		StringBuilder sbSql=new StringBuilder(String.format(UPDATE,tableName,SqlTemplate.getExecuteSql(sqlSet)));
		if(wrapper.nonEmptyOfWhere()) {
			String sql=wrapper.getSqlNormal();
			sbSql.append(String.format(WHERE,SqlTemplate.getExecuteSql(sql)));
		}
		sbSql.append(wrapper.getSqlNormalAfter());
		return sbSql.toString();
	}
	
	public static Object[] getParams(UpdateWrapper wrapper) {
		Map<String,Object> params=wrapper.getParamNameValuePairs();
		StringBuilder sbSql=new StringBuilder(wrapper.getSqlSet());
		if(!StringUtils.isEmpty(wrapper.getSqlSegment())) {
			sbSql.append(wrapper.getSqlSegment());
		}
		return SqlTemplate.getExecuteParameter(sbSql.toString(), params);
	}
	
	public static String getSql(String tableName,DeleteWrapper wrapper) {
		StringBuilder sbSql=new StringBuilder(String.format(DELETE,tableName));
		if(wrapper.nonEmptyOfWhere()) {
			String sql=wrapper.getSqlNormal();
			sbSql.append(String.format(WHERE,SqlTemplate.getExecuteSql(sql)));
		}
		sbSql.append(wrapper.getSqlNormalAfter());
		return sbSql.toString();
	}
	
	public static Object[] getParams(DeleteWrapper wrapper) {
		Map<String,Object> params=wrapper.getParamNameValuePairs();
		return SqlTemplate.getExecuteParameter(wrapper.getSqlSegment(), params);
	}
	
	public static void main(String[] args) {
		QueryWrapper wrapper=new QueryWrapper();
		wrapper.eq("name", "姓名").eq("age", 15).ge("starttime", new Date()).and(t->t.apply("select * from sys_configure where code={0} and ee={1}", "usekaaa","u111"));
		wrapper.groupBy("age").orderByAsc("name").last("limit 0,20");
		System.out.println(getSql("sys_user", wrapper));
		System.out.println(getSql("sys_user", wrapper,false));
		for(Object o:getParams(wrapper)) {
			System.out.println(o);
		}
	}
	
}
